package com.hegongda.dao.impl;

import com.hegongda.bean.Poster;
import com.hegongda.dao.PosterDao;
import com.hegongda.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class PosterDaoImpl implements PosterDao {
    @Override
    public Poster getOnePoster(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select * from poster where id = ?";
        return qr.query(sql, new BeanHandler<Poster>(Poster.class), id);
    }

    @Override
    public int addPoster(String pid, String path,String id) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String str = "insert poster values (?,?,?)";
        return qr.update(str, id,pid, path);
    }

    @Override
    public int EditPoster(String id, String path) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql= "update poster set pimage=? where id=?";
        return qr.update(sql,path,id);
    }

    @Override
    public List<Poster> showAll() throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT * FROM poster ORDER BY id";
        List<Poster> query = qr.query(sql, new BeanListHandler<Poster>(Poster.class));
        return query;
    }

    @Override
    public int del(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql= "delete from poster where id=?";
        return qr.update(sql,id);
    }
}
